package custom_view;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageButton;

import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;
import com.wefika.flowlayout.FlowLayout;

import java.util.ArrayList;

import adapter.group_management.AddMemberToGroupAdapter;
import controller.group_management.DownloadCircleImageViewInMemberTabCircleImageViewAsync;
import de.hdodenhof.circleimageview.CircleImageView;
import define.API;
import ui.fragment.group_management.CreateGroupFragment;

/**
 * Created by trek2000 on 23/8/2014.
 */
public class MemberTabFrameLayout extends FrameLayout {
    /**
     * String session
     */
    public static String AVATAR = "";
    /**
     * Views session
     */
    public static CircleImageView circleIvInMemberTabCircleImageView;
    public static ImageButton mIbtnRemoveInMemberTabCircleImageView;
    /**
     * The others secssion
     */
    public static Context mContext;

    public static MemberTabFrameLayout newInstance(Context context, String avatar, int tag ) {
        mContext = context;
        AVATAR = avatar;
        MemberTabFrameLayout memberTabFrameLayout =
                new MemberTabFrameLayout(context);
//        memberTabFrameLayout.setTag(tag);
        memberTabFrameLayout.addView(circleIvInMemberTabCircleImageView);
        memberTabFrameLayout.addView(mIbtnRemoveInMemberTabCircleImageView);
        return memberTabFrameLayout;
    }

    public MemberTabFrameLayout(Context context) {
        super(context);
        // Set background color
        setBackgroundColor(context.getResources().getColor(R.color.transparent));

        // Layout params for margin
        FlowLayout.LayoutParams layoutParams = new FlowLayout.LayoutParams(
                150, 150);
        layoutParams.setMargins(10, 5, 10, 5);
        setLayoutParams(layoutParams);
        // Set padding
        setPadding(5, 0, 5, 0);
        //init views
        initialViews ();
        //load circle image view
        loadCircleImageView(AVATAR);
        //layout gravity for image button remove
        setGravityForImageButtonRemove();
        // onclick for image button remove
        onClickImageButtonRemove();
    }

    private void loadCircleImageView (String avatar) {
        FrameLayout.LayoutParams layoutParamsForCircleImageView = new FrameLayout.LayoutParams(
                FlowLayout.LayoutParams.MATCH_PARENT, FlowLayout.LayoutParams.MATCH_PARENT);
        layoutParamsForCircleImageView.gravity = Gravity.CENTER;
        layoutParamsForCircleImageView.setMargins(5, 5, 5, 5);
        circleIvInMemberTabCircleImageView.setLayoutParams(layoutParamsForCircleImageView);
        try {
            String URL = "";
            if (API.IS_DEV_SITE)
                URL = avatar.replace("https", "http");
            else
                URL = avatar;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                new DownloadCircleImageViewInMemberTabCircleImageViewAsync
                        (mContext, circleIvInMemberTabCircleImageView).
                        executeOnExecutor(
                                AsyncTask.THREAD_POOL_EXECUTOR, URL);
            else
                new DownloadCircleImageViewInMemberTabCircleImageViewAsync
                        (mContext, circleIvInMemberTabCircleImageView).
                        execute(URL);
        } catch (Exception e) {
            e.printStackTrace();;
        }
    }
    private void setGravityForImageButtonRemove() {
        FrameLayout.LayoutParams layoutParamsForImageButtonRemove = new FrameLayout.LayoutParams(
                50, 50);
        layoutParamsForImageButtonRemove.gravity = Gravity.TOP|Gravity.RIGHT;
        layoutParamsForImageButtonRemove.setMargins(0, 5, 5, 0);
        mIbtnRemoveInMemberTabCircleImageView.setLayoutParams(layoutParamsForImageButtonRemove);
        mIbtnRemoveInMemberTabCircleImageView.setBackgroundResource(R.drawable.ibtn_remove);
    }

    private void onClickImageButtonRemove() {
        mIbtnRemoveInMemberTabCircleImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 * Flowlayout add MemberTabCircleImageView(Framelayout), MemberTabCircleImageView add image button
                 * image button set onclick
                 * v.getParent() => MemberTabCircleImageView(Framelayout), v.getParent().getParent() => Flowlayout
                 */
                switch(((FlowLayout) v.getParent().getParent()).getId()){
                    case R.id.flowlayout_add_member_to_group_manager_in_create_group_fragment:
                        Log.i("", "Group manager ((FrameLayout) v.getParent()).getTag() " + ((FrameLayout) v.getParent()).getTag());
                        //remove view group manager
                        CreateGroupFragment.removeMemberTabForGroupManger(Integer.valueOf(((FrameLayout) v.getParent()).getTag().toString()));
                        //remove data group manager
                        ArrayList <Integer> mAlTemplate;
                        mAlTemplate = Enterprise.groupManagement.getmAlPosOfItemCheckedInAddGroupManager();
                        mAlTemplate.remove(mAlTemplate.get(Integer.valueOf(((FrameLayout) v.getParent()).getTag().toString())));
                        Log.i("", "mAlTemplate " + mAlTemplate.size());
                        Enterprise.groupManagement.setmAlPosOfItemCheckedInAddGroupManager(
                                mAlTemplate
                        );
                        //Show ibtn add group manager after remove group manager
                        if (Enterprise.groupManagement.getmAlPosOfItemCheckedInAddGroupManager() != null &&
                                Enterprise.groupManagement.getmAlPosOfItemCheckedInAddGroupManager().size() == 0) {
                            CreateGroupFragment.mIbtnAddMemberToGroupManagerInCreateGroupFragment.setVisibility(View.VISIBLE);
                        }

                        Log.i("","manager");
                        break;
                    case R.id.flowlayout_add_member_to_group_in_create_group_fragment:
                        Log.i("", "Member ((FrameLayout) v.getParent()).getTag() " + ((FrameLayout) v.getParent()).getTag());
                        //remove view member
                        CreateGroupFragment.removeMemberTabForMember(Integer.valueOf(((FrameLayout) v.getParent()).getTag().toString()));
                        //remove data member
                        ArrayList <Integer> mAlTemplates;
                        mAlTemplates = Enterprise.groupManagement.getmAlPosOfItemCheckedInAddMemberToGroup();
                        mAlTemplates.remove(mAlTemplates.get(Integer.valueOf(((FrameLayout) v.getParent()).getTag().toString())));
                        Log.i("", "mAlTemplates " + mAlTemplates.size());
                        Enterprise.groupManagement.setmAlPosOfItemCheckedInAddMemberToGroup(
                                mAlTemplates
                        );
                        Log.i("","CreateGroupFragment.mflowlayoutMemberContentInCreateGroupFragment.getChildCount()" +  CreateGroupFragment.mflowlayoutMemberContentInCreateGroupFragment.getChildCount());
                        //reset tag after remove 1 item in mflowlayoutMemberContentInCreateGroupFragment
                        for (int t = Integer.valueOf(((FrameLayout) v.getParent()).getTag().toString()) ;
                             t < CreateGroupFragment.mflowlayoutMemberContentInCreateGroupFragment.getChildCount(); t++ ) {
                            CreateGroupFragment.mflowlayoutMemberContentInCreateGroupFragment.getChildAt(t).setTag(t);
                        }
                        Log.i("","member");
                        break;
                }

            }
        });
    }

    private void initialViews () {
        circleIvInMemberTabCircleImageView = new CircleImageView(mContext);
        mIbtnRemoveInMemberTabCircleImageView = new ImageButton(mContext);
    }
}
